import React, {useState} from "react"
import {get} from "../../../shared/server"

export const validateHook = (params) => {
	const [loginNameStatus, setLoginNameStatus] = useState({});
	const [phoneNoStatus, setPhoneNoStatus] = useState({});

	const loginNameValidate = (_, value, callback) => {
		setLoginNameStatus({validateStatus: 'validating'})
		get(`/management/account/admin/loginName/checkDuplicate`, {
			loginName: value,
			...params
		}).then(result => {
			if (result === true) {
				callback(`登录名${value}已经存在,请使用其它登录名!`)
			}
			callback()
		}).finally(() => setLoginNameStatus({}))
	}

	const phoneValidate = (_, value, callback, id) => {
		setPhoneNoStatus({validateStatus: 'validating'})
		let postData;
		postData = {
			phoneNo: value,
			...params
		}
		get(`/management/account/admin/phoneNo/checkDuplicate`, postData).then(result => {
			if (result === true) {
				callback(`手机号${value}已经存在,请使用其它手机号!`)
			}
			callback()
		}).finally(() => setPhoneNoStatus({}))
	}
	return {
		loginNameValidate,
		phoneValidate,
		loginNameStatus,
		phoneNoStatus,
		setLoginNameStatus,
		setPhoneNoStatus,
	}
}